/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import java.util.List;
import models.Course;
import models.ForumAnswer;
import models.ForumQuestion;
import models.User;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Aster
 */
public class ForumDao {
    private final Session session;
    
    public ForumDao(Session session) {
        this.session = session;
    }
    
    public List<ForumQuestion> findAll() {
        Transaction tx = session.beginTransaction();
        List<ForumQuestion> list = session.createQuery("from ForumQuestion").list();
        tx.commit();
        return list;
    }
    
    public void save(ForumQuestion question) {
        Transaction tx = session.beginTransaction();
        session.save(question);
        tx.commit();
    }

    public void update(ForumQuestion question) {
        Transaction tx = session.beginTransaction();
        session.update(question);
        tx.commit();
    }

    public ForumQuestion get(long id) {
        return (ForumQuestion) session.get(ForumQuestion.class, id);
    }
    
    public void delete(ForumQuestion question) {
        Transaction tx = session.beginTransaction();
        session.delete(question);
        tx.commit();
    }
    
    public List<ForumAnswer> findAllAnswers() {
        Transaction tx = session.beginTransaction();
        List<ForumAnswer> list = session.createQuery("from ForumAnswer").list();
        tx.commit();
        return list;
    }
    
    public void saveAnswer(ForumAnswer answer) {
        Transaction tx = session.beginTransaction();
        session.save(answer);
        tx.commit();
    }

    public void updateAnswer(ForumAnswer answer) {
        Transaction tx = session.beginTransaction();
        session.update(answer);
        tx.commit();
    }

    public ForumAnswer getAnswer(long id) {
        return (ForumAnswer) session.get(ForumAnswer.class, id);
    }
    
    public void delete(ForumAnswer answer) {
        Transaction tx = session.beginTransaction();
        session.delete(answer);
        tx.commit();
    }
}
